pre = r'pre.fa'
mat = r'mature.fa'
outfile = r'mirbase_Ref'

from Bio import SeqIO

dic = {}

class miR(object):
    def __init__(self,name):
        self.name= name
        self.pre = ''
        self.mat_5p = ''
        self.mat_3p = ''

for i in SeqIO.parse(pre,'fasta'):
    dic[str(i.name).lower()] = miR(str(i.name).lower())
    dic[str(i.name).lower()].pre = str(i.seq)

for i in SeqIO.parse(mat,'fasta'):
    k = str(i.name).lower().split('-')
    if k[-1] in ['5p','3p','5p*','3p*']:
        name = '-'.join(k[:-1])
    else:
        name = str(i.name).lower()
    if name in dic:
        seq = str(i.seq)
        p = dic[name].pre.find(seq)
        if p!= -1:
            if p < int(len(dic[name].pre)/2.2):
                dic[name].mat_5p = seq
            elif p> int(len(dic[name].pre)/2.2):
                dic[name].mat_3p = seq
with open(outfile,'w') as fila:
    for i in dic:
        fila.write(dic[i].name+'\t')
        if dic[i].mat_5p!= '':
            fila.write(dic[i].mat_5p+'\t')
        else:
            fila.write('-'+'\t')
        if dic[i].mat_3p!= '':
            fila.write(dic[i].mat_3p+'\t')
        else:
            fila.write('-'+'\t')
        fila.write(dic[i].pre+'\n')
